Imports EnvDTE
Imports EnvDTE80


Class TemplatePowerTool
    Inherits BaseTool


    Public Sub New()
    End Sub

    Public Const TemplateFileType As String = ".html"

    Public Overrides Function AvailableItems(ByVal project As EnvDTE.Project) As System.Collections.Generic.List(Of String)

        Dim username As String = ProjectOptionsManager.UmbracoUser(project)
        Dim password As String = ProjectOptionsManager.UmbracoPassword(project)

        Dim templateService As New Data.TemplateService.templateService
        templateService.Url = GetServiceURL(project)

        Dim items As New Generic.List(Of String)
        For Each item As Data.TemplateService.templateCarrier In templateService.readList(username, password)
            items.Add(item.Alias & TemplateFileType)
        Next

        Return items
    End Function

    Public Function GetServiceURL(ByVal project As Project) As String
        Return ProjectOptionsManager.UmbracoSite(project) & "/webservices/TemplateService.asmx"
    End Function

    Public Overrides Sub DownloadItem(ByVal projectItem As EnvDTE.ProjectItem)

        Dim project As Project = projectItem.ContainingProject
        Dim templateService As New Data.TemplateService.templateService
        templateService.Url = GetServiceURL(project)

        Dim username As String = ProjectOptionsManager.UmbracoUser(project)
        Dim password As String = ProjectOptionsManager.UmbracoPassword(project)
        Dim fileName As String = projectItem.Name
        Dim fileLocation As String = projectItem.FileNames(1)
        Dim data() As Byte = Nothing

        Dim templateAlias As String = fileName.Substring(0, fileName.Length - TemplateFileType.Length)
        Dim templateId As Integer
        Try
            templateId = templateService.readIdFromAlias(templateAlias, username, password)
        Catch ex As Exception
            MsgBox("Could not find item with alias: " & templateAlias)
            Exit Sub
        End Try

        Dim carrier As Data.TemplateService.templateCarrier

        Try
            carrier = templateService.read(templateId, username, password)
        Catch ex As Exception
            MsgBox("Could not load template with id: " & templateId & ", " & ex.Message)
            Exit Sub
        End Try

        data = System.Text.Encoding.UTF8.GetBytes(carrier.Design)

        Dim fileStream As New IO.FileStream(fileLocation, IO.FileMode.Create, IO.FileAccess.ReadWrite)
        fileStream.Write(data, 0, data.Length)
        fileStream.Close()

    End Sub

    Public Overrides Sub UploadItem(ByVal projectItem As EnvDTE.ProjectItem)

        Dim project As Project = projectItem.ContainingProject
        Dim templateService As New Data.TemplateService.templateService
        templateService.Url = GetServiceURL(project)

        Dim username As String = ProjectOptionsManager.UmbracoUser(project)
        Dim password As String = ProjectOptionsManager.UmbracoPassword(project)
        Dim fileName As String = projectItem.Name
        Dim fileLocation As String = projectItem.FileNames(1)
        Dim data() As Byte = Nothing

        Dim templateAlias As String = fileName.Substring(0, fileName.Length - TemplateFileType.Length)
        Dim templateId As Integer
        Try
            templateId = templateService.readIdFromAlias(templateAlias, username, password)
        Catch ex As Exception
            MsgBox("Could not find item with alias: " & templateAlias)
            Exit Sub
        End Try

        Dim carrier As Data.TemplateService.templateCarrier

        Try
            carrier = templateService.read(templateId, username, password)
        Catch ex As Exception
            MsgBox("Could not load template with id: " & templateId & ", " & ex.Message)
            Exit Sub
        End Try


        Dim file As System.IO.FileInfo = New System.IO.FileInfo(fileLocation)
        Dim filestream As IO.StreamReader
        filestream = file.OpenText
        carrier.Design = filestream.ReadToEnd()
        filestream.Close()

        templateService.update(carrier, username, password)

    End Sub

    Public Overrides ReadOnly Property ToolDescription() As String
        Get
            Return "Tool to edit templates in umbraco"
        End Get
    End Property

    Public Overrides ReadOnly Property ToolName() As String
        Get
            Return "Template Tool"
        End Get
    End Property



End Class
